﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESpace;

namespace ESpace_Project
{
    public partial class FormEMath : Form
    {
         Point p1 = new Point(-1, -1);
        Point p2 = new Point(-1, -1);
        Point p3 = new Point(-1, -1);

        public FormEMath()
        {
            InitializeComponent();
            im1.Image = new Bitmap(im1.Width, im1.Height);
            im2.Image = new Bitmap(im2.Width, im2.Height);
            im3.Image = new Bitmap(im3.Width, im3.Height);

            p1 = new Point(-1, -1);
            p2 = new Point(-1, -1);
            p3 = new Point(-1, -1);

            DrawAll1(150, 150);
            DrawAll2();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
          
        }

        private void im1_MouseDown(object sender, MouseEventArgs e)
        {
            DrawAll1(e.X, e.Y);
        }

        Random rnd = new Random();

        void DrawAll1(int X, int Y)
        {
            Graphics gr = Graphics.FromImage(im1.Image);
            gr.Clear(Color.White);

            gr.FillEllipse(Brushes.Maroon, 150 - 2, 150 - 2, 4, 4);
            gr.DrawLine(Pens.Black, 150, 150, X, Y);

            // проверка по времени
            DateTime dt = DateTime.Now;
           /* for (int i = 0; i < 1000000; i++)
            {
                EMath.GetAngle(150, 150, rnd.Next(300), rnd.Next(300)).ToString();
            }

            label6.Text = (DateTime.Now - dt).TotalMilliseconds.ToString();

            dt = DateTime.Now;
            for (int i = 0; i < 1000000; i++)
            {
                EMath.GetAngle1(new Point(150, 150), new Point(X, Y)).ToString();
            }
            label7.Text = (DateTime.Now - dt).TotalMilliseconds.ToString();*/

            label6.Text = (new Point(X, Y)).ToString();

            double len = EMath.GetLen(new Point(150, 150), new Point(X,Y)); 
            double angle = EMath.GetAngle(new Point(150, 150), new Point(X, Y));
            label7.Text = len.ToString();

            Point p = EMath.GetPoint(new Point(150, 150), len, angle);
            label8.Text = p.ToString();
            label9.Text = EMath.GetLen(new Point(150, 150), p).ToString();

            gr.Dispose();
            im1.Refresh();
        }

        //******************************************************************************
        //******************************************************************************
        //******************************************************************************

       

        void DrawAll2()
        {
            Graphics gr = Graphics.FromImage(im2.Image);
            gr.Clear(Color.White);

            if (p1.X != -1)
            {
                gr.FillEllipse(Brushes.Navy, p1.X - 2, p1.Y - 2, 5, 5);
                gr.DrawString("A", Font, Brushes.Blue, p1.X + 5, p1.Y);
            }
            if (p1.X != -1 & p2.X != -1)
            {
                gr.FillEllipse(Brushes.Navy, p2.X - 2, p2.Y - 2, 5, 5);
                gr.DrawString("B", Font, Brushes.Blue, p2.X + 5, p2.Y);
                gr.DrawLine(Pens.Red, p1, p2);
            }
            if (p1.X != -1 & p2.X != -1 & p3.X != -1)
            {
                gr.FillEllipse(Brushes.Navy, p3.X - 2, p3.Y - 2, 5, 5);
                gr.DrawString("C", Font, Brushes.Blue, p3.X + 5, p3.Y);
                gr.DrawLine(Pens.Red, p1, p3);
                gr.DrawLine(Pens.Red, p2, p3);
            }

            if (p1.X != -1 & p2.X != -1 & p3.X != -1)
            {
                EMath.aT at = EMath.AnglesTriangle(p1, p2, p3);
                gr.DrawString("A " + (Math.Round(at.a, 1)).ToString(), Font, Brushes.Blue, p1.X + 5, p1.Y);
                gr.DrawString("B " + (Math.Round(at.b, 1)).ToString(), Font, Brushes.Blue, p2.X + 5, p2.Y);
                gr.DrawString("C " + (Math.Round(at.c, 1)).ToString(), Font, Brushes.Blue, p3.X + 5, p3.Y);
            }

            gr.Dispose();
            im2.Refresh();
        }


        private void im2_MouseDown(object sender, MouseEventArgs e)
        {
            if (p1.X != -1 & p2.X != -1 & p3.X != -1)
            {
                p1.X = -1;
                p2.X = -1;
                p3.X = -1;
            }

            if (p1.X == -1)
            {
                p1.X = e.X;
                p1.Y = e.Y;
            }
            else if (p2.X == -1)
            {
                p2.X = e.X;
                p2.Y = e.Y;

                label5.Text = EMath.GetAngle(p1, p2).ToString();
            }
            else
            {
                p3.X = e.X;
                p3.Y = e.Y;
            }
            DrawAll2();
        }


        // тестирование относительных координат
        private void txbX_TextChanged(object sender, EventArgs e)
        {
            Graphics gr = Graphics.FromImage(im3.Image);
            gr.Clear(Color.White);

            bool bOk = true;
            int X = 0; if (!int.TryParse(txbX.Text.Trim(), out X)) bOk = false;
            int Y = 0; if (!int.TryParse(txbY.Text.Trim(), out Y)) bOk = false;
            int A = 0; if (!int.TryParse(txbAngle.Text.Trim(), out A)) bOk = false;

            if (bOk)
            {
                Point pnt = EMath.OtnCoord(new Point(100, 100), new Point(X, Y), A);
                gr.DrawLine(Pens.Black, 100, 100, pnt.X, pnt.Y);
            }

            gr.FillEllipse(Brushes.Red, 99, 99, 3, 3);

            gr.Dispose();
            im3.Refresh();
        }
    }
}
